// 计数器
let interval = null
// 值越大旋转时间约长
let intime = 50
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [
      {
        opa: 0.9,
        imgUrl: 'http://www.modo5.com/images/gift.png',
        name: "iphone12"
      },
      {
        opa: 0.9,
        imgUrl: 'http://www.modo5.com/images/gift.png',
        name: "再接再厉"
      },
      {
        opa: 0.9,
        imgUrl: 'http://www.modo5.com/images/gift.png',
        name: "再接再厉"
      },
      {
        opa: 0.9,
        imgUrl: 'http://www.modo5.com/images/gift.png',
        name: "iPad"
      },
      {
        opa: 0.9,
        imgUrl: 'http://www.modo5.com/images/gift.png',
        name: "再接再厉"
      },
      {
        opa: 0.9,
        imgUrl: 'http://www.modo5.com/images/gift.png',
        name: "再接再厉"
      },
      {
        opa: 0.9,
        imgUrl: 'http://www.modo5.com/images/gift.png',
        name: "iPad"
      },
      {
        opa: 0.9,
        imgUrl: 'http://www.modo5.com/images/gift.png',
        name: "再接再厉"
      },
    ],
    luckPosition:'',
    isDisabled: false
  },
  //点击抽奖按钮
  startLucks() {
    console.log(`startLucks`)
    // if(!this.data.lotteryFlag){
    //   return
    // }
    this.setData({
      luckPosition:'',
      // lotteryFlag:false,
    })
    let setPrize=Math.floor(Math.random()*2)
    console.log(setPrize)
    if(setPrize==1){
      setPrize=2
    }
    let realPrize=setPrize
    this.setData({
      luckPosition:realPrize
    })
    let vm = this
    let {
      luckPosition,
      list,
      isDisabled
    } = vm.data
    if (isDisabled){
      return
    }
    if (!isDisabled) {
      //判断中奖位置格式
      if (luckPosition == null || isNaN(luckPosition) || luckPosition > 7) {
        wx.showModal({
          title: '提示',
          content: '系统发生错误，请稍后重试',
          showCancel: false
        });
        return;
      }

      //设置按钮不可点击
      vm.setData({
        isDisabled: true
      })

      //清空计时器
      clearInterval(interval);
      let index = 0;
      //循环设置每一项的透明度
      interval = setInterval(() => {
        if (index > 7) {
          index = 0;
          list[7].opa = 0.9
        } else if (index != 0) {
          list[index - 1].opa = 0.9
        }
        list[index].opa = 1;
        index++;
        vm.setData({
          list
        })
      }, intime);
      //模拟网络请求时间  设为两秒
      var stoptime = 2000;
      setTimeout(() => {
        vm.stop(luckPosition);
      }, stoptime)
    }
  },
  stop(which) {
    let vm = this;
    let {
      list
    } = vm.data

    //清空计数器
    clearInterval(interval);
    //初始化当前位置
    let current = -1;

    for (let i = 0; i < list.length; i++) {
      if (list[i].opa == 1) {
        current = i;
      }
    }
    //下标从1开始
    let index = current + 1;
    vm.stopLuck(which, index, intime, 2);
  },
  stopLuck(which, index, time, splittime) {
    let vm = this;
    let { list } = this.data
    //值越大出现中奖结果后减速时间越长
    setTimeout(() => {
      //重置前一个位置
      if (index > 7) {
        index = 0;
        list[7].opa = 0.9
      } else if (index != 0) {
        list[index - 1].opa = 0.9
      }
      //当前位置为选中状态
      list[index].opa = 1;
      vm.setData({
        list
      })
      //如果旋转时间过短或者当前位置不等于中奖位置则递归执行
      //直到旋转至中奖位置
      if (time < 400 || index != which) {
        //越来越慢
        splittime++;
        time += splittime;
        //当前位置+1
        index++;
        vm.stopLuck(which, index, time, splittime);
      } else {
        //1秒后显示弹窗
        setTimeout(() => {
          if (which == 0) {
            //中奖
            wx.showModal({
              content: '恭喜获得：' + list[which].name,
              showCancel: false,
              confirmColor: "#F8C219",
              success: res => {
                if (res.confirm) {
                  //设置按钮可以点击
                  vm.setData({
                    isDisabled: false
                  })
                  vm.loadAnimation();
                }
              }
            });
          } else {
            //未中奖
            wx.showModal({
              content: '很遗憾未中奖',
              showCancel: false,
              confirmColor: "#F8C219",
              success: res => {
                if (res.confirm) {
                  //设置按钮可以点击
                  vm.setData({
                    isDisabled: false
                  })
                  vm.loadAnimation();
                }
              }
            });
          }
        }, 1000);
      }
    }, time);
  },
  loadAnimation() {
    let vm = this;
    let index = 0;
    let {
      list
    } = vm.data

    clearInterval(interval)
    interval = setInterval(() => {
      if (index > 7) {
        index = 0;
        list[7].opa = 0.9
      } else if (index != 0) {
        list[index - 1].opa = 0.9
      }
      list[index].opa = 1;
      index++;
      vm.setData({
        list
      })
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadAnimation();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    //清空计时器
    clearInterval(interval);
  }
})